﻿
function ExpressionParser(){
	this.fun = new FunctionLib();
	//this.exception = common.ObjectFactory.getInstance().getException();
}

ExpressionParser.prototype = {
	calculate:function(expression){
		if(CalculateUtil.isNull(expression)){
			return "0";
			//this.exception.throwException("ExpressionParser.calculate():表达式为空");
		}
		var leftBracketIndex = expression.indexOf("(");
		if(leftBracketIndex==-1){//简单的四则计算
			if(CalculateUtil.isNumberAndPoint(expression)){//数字直接返回
				//this.exception.debug("数字表达式");
				return expression;
			}else if(CalculateUtil.isBoolean(expression)){//是boolean值也直接返回
				//this.exception.debug("boolean表达式");
				return CalculateUtil.getBoolean(expression);
			}else if(CalculateUtil.isContainOperator(expression)){//四则运算
				//this.exception.debug("四则运算表达式");
				var fun = this.fun.funMap.get("sum");
				return fun.calculate(expression);
			}else if(CalculateUtil.isContainLogicOrBoolean(expression)){//逻辑运算
				//this.exception.debug("逻辑运算表达式");
				var fun = this.fun.funMap.get("if");
				return fun.calculate(expression);
			}else{
				//this.exception.debug("未知运算表达式:"+expression);
				return 0;
			}
			
		}else if(expression.indexOf(";")!=-1){//多个表达式：if();if();
			var expressionArr = expression.split(";");
			for(var i=0;i<expressionArr.length;i++){
				var expressionTemp = expressionArr[i];
				var value = this.calculate(expressionTemp);//递归计算每个表达式
				//this.exception.debug("ifValue = " + value);
				if(value!=null && value!=0){
					return value;
				}
			}
			return null;
		}else{
			//寻找左右括号的配对
			var leftBrackets = 0;
			var rightBrackets = 0;
			var rightBracketIndex = 0;
			var i=leftBracketIndex;
			for(;i<expression.length;i++){
				var c = expression.charAt(i);
				if(c=='('){
					if(leftBrackets==rightBrackets){
						leftBracketIndex = i;
					}
					leftBrackets++;
				}else if(c==')'){
					rightBrackets++;
					if(leftBrackets==rightBrackets){
						rightBracketIndex = i;
						i = leftBracketIndex;//重新设置寻找括号的起点
						
						//将括号内的表达式用值替换掉
						var lastOper = -1;//必须为-1,如果没有，就从开头算起
						for(var j=leftBracketIndex-1;j>=0;j--){//判断括号前面的是不是基本函数
							var temp = expression.charAt(j);
							if(CalculateUtil.isOperatorOrBracket(temp) || CalculateUtil.isLogic(temp) || temp==' '){
								lastOper = j;
								break;
							}
						}
						var expression2 = expression.substring(leftBracketIndex+1,rightBracketIndex);
						//this.exception.debug("括号内的表达式 = " + expression2);
						var value = expression2;
						var funName = expression.substring(lastOper+1,leftBracketIndex);
						//this.exception.debug("函数名称 = " + funName );
						if(!CalculateUtil.isNull(funName) && !this.fun.isFunction(funName)){
							throw new Error("ExpressionParser.calculate():"+funName + " 不是函数");
						}
						if(this.fun.isFunction(funName)){//是基本函数
							var fun = this.fun.funMap.get(funName);
								
							var paraValues = expression2;
							if(fun.isCal){
								paraValues = "";
								var tempArr = expression2.split(",");
								//计算函数里每个参数的值
								for(var k=0;k<tempArr.length;k++){
									var para = tempArr[k].trim();
									var paraValue = this.calculate(para);//递归获取参数的值
									paraValues += paraValue + ",";
								}
								paraValues = paraValues.substring(0,paraValues.length-1);
							}
							//this.exception.debug("函数内的计算值 = " + paraValues);
							
							var value2 = fun.calculate(paraValues);
							//this.exception.debug("函数内的参数值 = " + value2);
							expression = expression.substring(0,lastOper+1)+value2+expression.substring(rightBracketIndex+1);
							//this.exception.debug("运算基本函数后的expression = " + expression);			
						}else{//不是基本函数
							value = this.calculate(expression2);//递归获取括号内的值
							expression = expression.substring(0,leftBracketIndex)+value+expression.substring(rightBracketIndex+1);
							//this.exception.debug("运算括号内容后的expression = " + expression);	
						}
						
					}
				}
			}
			if(leftBrackets!=rightBrackets){
				throw new Error("ExpressionParser.calculate():左右括号不配对");
			}
			var value = this.calculate(expression);
			return value;
		}
	}
}

